﻿using Microsoft.AspNetCore.Mvc;
using WebHome_API.Models;
using WebHome_API.Models.DataModels;
using WebHome_API.Models.DBModels;
using WebHome_API.Models.OperResult;
using WebHome_API.Tools;

namespace WebHome_API.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class OtherController : ControllerBase
    {
        #region Notice
        [HttpGet("getNoticeList")]
        public OperResult<List<Notice_Table>> getNoticeList()
        {
            using (MyContext context = new())
            {
                var notices = context.Notice.Where(u => u.IsDel == 0).OrderByDescending(u => u.ID).Take(10).ToList();
                return OperResult<List<Notice_Table>>.Succeed(notices);
            }
        }

        [HttpGet("getNoticeOfNew")]
        public OperResult<Notice_Table> getNoticeOfNew()
        {
            using (MyContext context = new())
            {
                var notices = context.Notice.OrderByDescending(u => u.ID).FirstOrDefault(u => u.IsDel == 0);
                return OperResult<Notice_Table>.Succeed(notices);
            }
        }
        #endregion

        #region message
        [HttpGet("getMessageNewCount")]
        public OperResult<int> getMessageNewCount(int id)
        {
            using (MyContext context = new())
            {
                var count = context.Messages.Where(u => u.IsDel == 0).Where(u => u.ID > id).Count();
                return OperResult<int>.Succeed(count);
            }
        }
        [HttpGet("getMessageList")]
        public OperResult<MessageList> getMessageList(int page, int perPage = 30)
        {
            if (page < 1)
                page = 1;
            using (MyContext context = new())
            {
                var all = context.Messages.Where(u => u.IsDel == 0).OrderByDescending(u => u.ID).ToList();
                return OperResult<MessageList>.Succeed(new MessageList(all, page, perPage));
            }
        }

        [HttpPost("loadMessage")]
        public OperResult<int> loadMessage(string name, string text)
        {
            if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(text))
            {
                return OperResult<int>.Failed("Content Is Null");
            }
            using (MyContext context = new())
            {
                var cur = new Messages_Table()
                {
                    Name = name,
                    Text = text,
                    IsDel = 0,
                    CreateTime = DateTime.Now,
                };
                context.Messages.Add(cur);
                if (context.SaveChanges() == 1)
                {
                    return OperResult<int>.Succeed(cur.ID);
                }
                else
                {
                    return OperResult<int>.Failed("Message Save Is Error");
                }
            }
        }

        [HttpPost("delMessage")]
        public OperResult delMessage(int id)
        {
            using (MyContext context = new())
            {
                var cur = context.Messages.FirstOrDefault(m => m.ID == id);
                if (cur != null)
                {
                    cur.IsDel = 1;
                }
                var ret = context.SaveChanges();
                if (ret == 1)
                    return OperResult.Succeed();
                else
                    return OperResult.Failed("Message Del Error!");
            }
        }
        #endregion


        [HttpGet("setPath")]
        public OperResult<string> setPath()
        {
            using (MyContext context = new())
            {
                var ip = context.OtherEnums.FirstOrDefault(u => u.ID == 1);
                if (ip == null)
                {
                    return OperResult<string>.Failed("地址更新失败, 查询不到ID=1的信息");
                }
                else
                {
                    ip.Text = $"http://[{Ipv6.getIpv6()}]:9000/";
                    var ret = context.SaveChanges();
                    if (ret == 1)
                    {
                        return OperResult<string>.Succeed("地址更成功");
                    }
                    else
                    {
                        return OperResult<string>.Succeed("地址不需要更新");
                    }
                }
            }

        }
    }
}
